

#include "file.h"
#include <cstdint>

void AppFile::InitFile(uint16_t type)
{
    switch ((FileType)type) {
        case FileType::File_PCM: {
            InitPCMFile();
            break;
        }
        case FileType::File_AAC: {
            InitAACFile();
            break;
        }
        default: {
            InitPCMFile();
            InitAACFile();
            break;
        }
    }
}

void AppFile::WriteFile(uint16_t type, void *buffer, int32_t len)
{
    switch ((FileType)type) {
        case FileType::File_PCM: {
            if (pcmFile_ != nullptr) {
                fwrite(buffer, len, 1, pcmFile_);
            }
            break;
        }
        case FileType::File_AAC: {
            if (aacFile_ != nullptr) {
                fwrite(buffer, len, 1, aacFile_);
            }
            break;
        }
    }
}

void AppFile::CloseFile(uint16_t type)
{
    switch ((FileType)type) {
        case FileType::File_PCM: {
            ClosePCMFile();
            break;
        }
        case FileType::File_AAC: {
            CloseAACFile();
            break;
        }
        default: {
            ClosePCMFile();
            CloseAACFile();
            break;
        }
    }
}

void AppFile::InitPCMFile()
{
    if (pcmFile_ == nullptr) {
        pcmFile_ = fopen(pcmPath_.c_str(), "ab+");  //追加
    }
}

void AppFile::InitAACFile()
{
    if (aacFile_ == nullptr) {
        aacFile_ = fopen(aacPath_.c_str(), "ab+");
    }
}

void AppFile::ClosePCMFile()
{
    if (pcmFile_ != nullptr) {
        fclose(pcmFile_);
        pcmFile_ = nullptr;
    }
}

void AppFile::CloseAACFile()
{
    if (aacFile_ != nullptr) {
        fclose(aacFile_);
        aacFile_ = nullptr;
    }
}